<template>
  <div v-loading="loading" element-loading-text="模型生成中...">
    <div style="width: 280px; border: 1px solid white;">
      <el-form  ref="form" label-width="90px">
        <el-form-item label="模数">
          <el-input size="small" onkeypress="return (event.charCode >= 48 && event.charCode <= 57) || event.charCode== 46" v-model.number="m" type="number" placeholder="请输入内容"></el-input>
        </el-form-item>
        <el-form-item label="齿数">
          <el-input size="small" onkeypress="return event.charCode >= 48 && event.charCode <= 57" v-model.number="z" type="number" placeholder="请输入内容"></el-input>
        </el-form-item>
        <el-form-item label="压力角">
          <el-select size="small" v-model="alpha">
            <el-option size="small" v-for="(item,index) in pressureAngleOption" :key="item" :label="item"
                       :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="齿高">
          <el-input size="small" onkeypress="return event.charCode >= 48 && event.charCode <= 57" v-model.number="h" type="number" placeholder="请输入内容">
            <i slot="suffix" style="font-style:normal; color: #606266;">mm</i>
          </el-input>
        </el-form-item>
        <el-form-item label="齿厚">
          <el-input size="small" onkeypress="return event.charCode >= 48 && event.charCode <= 57" v-model.number="t" type="number" placeholder="请输入内容">
            <i slot="suffix" style="font-style:normal; color: #606266;">mm</i>
          </el-input>
        </el-form-item>
      </el-form>
      <div>
        <img src="/image/spline.png" height="135" width="280"/>
        <div style="margin-top: 10px;text-align: center;">
          <el-button type="primary" size="small" @click="creatSpline">确定</el-button>
          <el-button size="small"  @click="closeDialog">取消</el-button>
        </div>
      </div>
    </div>


  </div>
</template>

<script>
  import {
    Axis,
    CrownCADPlugin,
    Direction, PluginCADToolFunctionType,
    PluginFunctionType,
    Point
  } from "../../../../nodeApp/static/lib/crowncad-plugin-sdk-0.0.1";
  export default {
    name: "DrumShapedSpline",
    data() {
      return {
        loading:false,
        m: 0.75,   // 模数
        z: 20,  // 牙数
        h: 0.8,   // 压力角
        t: 10,  // 齿根
        alpha: 30,   // 压力角
        pressureAngleOption: [30,37.5,45], //压力角字典
        crownCADPlugin: null,
        currentPickElement:null,
        db:0,
        d: 0, //分度圆直径D = m*z
        df:0,
        dee: 0, //平齿根大径Dee = m*(z+1)
        die: 0,  //平齿根小径Die = m*(z-1.5)
        hs:0, //DFemax计算中的参数
        q1: {}, //渐开线与分度圆交点
        q2: {}, //内花键两天渐开线交点
        mP:0, // 以渐开线与分度圆交点构建内嵌多边形，多边形一边的中点，该中点构建镜像线
        EquationId:0, // 第一条渐开线id
        EquationP: {}, // 第一条渐开线终点
        EquationS:{}, // 第一条渐开线起点
        EquationId1:0, // 第二条渐开线id
        EquationP1: {}, // 第二条渐开线终点
        EquationS1:{}, // 第二条渐开线起点
        axisId:0, // 镜像轴id
        axisLen:0, // 镜像轴长度
        id1: [], // 分度圆id
        id2: [], // 内嵌多边形除了与镜像轴交点的所有边的id
        deeid: [], // 平齿根大径Dee的id
        dieid:[], // 平齿根小径Die的id
        dfid:[],  //鼓形齿辅助圆
        poel:[], //构建圆齿根时，存储小径或者大径（平齿根和圆齿根）与两条渐开线交点的那两段曲线
        p1: null, //poel曲线的中点
        id3: [],  //上视基准面上和P1交点形成的直线id
        id4:[], //上视基准面上的三点圆弧
        axis: null // 旋转凸台和阵列的中心轴
      }
    },
    mounted() {
      //创建插件对象
      this.crownCADPlugin = new CrownCADPlugin();
      //设置CrownCAD平台地址
      this.crownCADPlugin.setParentOrigin(window.url);
      //.设置与CrownCAD平台尝试建立连接的次数
      this.crownCADPlugin.setMaxAttempt(5);
      //设置插件在CrownCAD平台显示的尺寸
      this.crownCADPlugin.setLayout(300, 300, 420);

      //.发送与CrownCAD平台建立连接的请求
      this.crownCADPlugin.connect().then(() => {
        this.crownCADPlugin.request.execFunction(PluginFunctionType.LAYOUT, {minWidth: 300, width: 300, maxHeight: 480})

      });
    },
    methods: {
      closeDialog() {
        this.crownCADPlugin.request.execFunction(PluginCADToolFunctionType.CLOSEACTIVECMD, {}).then(()=> {

        });
      },
      // 准备大径小径等画草图数据
      updateData() {
        this.pressureAngle = (this.alpha / 180) * Math.PI;
        this.db = this.m * this.z * Math.cos(this.pressureAngle);
        this.d = this.m * this.z;
        this.hs = 0.6 * this.m;
        this.dee = this.m * (this.z + 1);
        this.die = this.m * (this.z - 1.5);
        this.df= 1.05* this.dee
        this.id1 = []
        this.id2 = []
        this.id3 = []
        this.id4 = []
        this.deeid = []
        this.dieid = []

        this.poel = []

      },
      creatSpline() {
        this.loading = true;

        //获取新配置，重新计算准备各个参数
        this.updateData()
        //清空command
        this.crownCADPlugin.command.clearCommand();
        //创建草图，命名为sketch1
        this.crownCADPlugin.command.sketch.createSketch({sketchName: 'sketch1', datumId: 8});

        //创建圆函数，草图中画几个基本圆
        this.createCircle()

        //画渐开线
        this.crownCADPlugin.command.sketch.createEquationCurve({
          equationType: 2,
          coordinateType: 1,
          equation1: 0.5* this.db + "*(sin(t) - t*cos(t))",
          equation2: 0.5* this.db + "*(cos(t) + t * sin(t))",
          startValue: 0,
          endValue: 1
        });

        //退出草图
        this.crownCADPlugin.command.sketch.exitSketch();
        //实现草图操作，参数true会覆盖之前的草图，不传则不会
        this.crownCADPlugin.command.execute(true).then(() => {
          this.crownCADPlugin.command.clearCommand();
          //获取上面画的圆和渐开线的信息
          this.getCircleAndEquationCurveInfo().then(()=>{
            //剪切分度圆，因为要取分度圆和渐开线的交点，分度圆裁剪后会剩两者的交点
            this.cutPitchCircle();
          });

        })
      },
      //创建圆函数，草图中画几个基本圆
      createCircle() {

        // 分度圆
        this.crownCADPlugin.command.sketch.createCircle({
          centerPoint: new Point(0,0,0),
          circlePoint: new Point(0,0.5* this.d,0),
        })


        this.crownCADPlugin.command.sketch.createCircle({
          centerPoint: new Point(0,0,0),
          circlePoint: new Point(0,0.5* this.dee,0),
        })


        this.crownCADPlugin.command.sketch.createCircle({
          centerPoint: new Point(0,0,0),
          circlePoint: new Point(0,0.5* this.die,0),
        })


        this.crownCADPlugin.command.sketch.createCircle({
          centerPoint: new Point(0,0,0),
          circlePoint: new Point(0,0.5* this.df,0),
        })

      },

      getCircleAndEquationCurveInfo() {
        return new Promise((resolve, reject) => {
          //根据草图名称sketch1，获取草图信息
          this.crownCADPlugin.query.getFeaturesByNames(['sketch1'])
          // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETFEATURESBYNAMES, {featureNames: ["sketch1"]})
            .then(res => {
              if (res?.data.features) {
                //获取草图sketch1中元素的信息
                this.crownCADPlugin.query.getElementsByFeatureId(res.data.features[0]?.id)
                // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETElEMENTSBYFEATURE, {featureId: res.data.features[0]?.id})
                  .then(result => {
                    result.data.elements.forEach(element => {
                      //获取渐开线信息，20类型为渐开线
                      if(element.type===8 && element.params.type=== 20) {
                        this.EquationId= element.id
                        this.EquationP= element.params.endPoint
                        this.EquationS= element.params.startPoint
                      }
                      //获取平齿根小径die的id
                      if(element.type===8 && element.params.type=== 11 && element.params.startPoint['0']=== 0.5* this.die) {
                        this.dieid.push(element.id)

                      }

                      //获取分度圆的id
                      if(element.type===8 && element.params.type=== 11 && element.params.startPoint['0']=== 0.5* this.d) {
                        this.id1.push(element.id)

                      }
                      //获取分度圆的id
                      if(element.type===8 && element.params.type=== 11 && element.params.startPoint['0']=== 0.5* this.dee) {
                        this.deeid.push(element.id)

                      }
                      //获取平齿根大径Dee的id
                      if(element.type===8 && element.params.type=== 11 && element.params.startPoint['0']=== 0.5* this.df) {
                        this.dfid.push(element.id)

                      }

                    })
                    resolve();
                  })

              } else {
                reject()
              }
            })
        })

      },

      //剪切分度圆，因为要取分度圆和渐开线的交点，分度圆裁剪后会剩两者的交点
      cutPitchCircle() {
        this.crownCADPlugin.command.clearCommand();
        //进入编辑操作
        this.crownCADPlugin.command.sketch.editSketch({sketchName: 'sketch1'});
        //裁剪分度圆
        this.crownCADPlugin.command.sketch.createAutoTrimCurve({
          trimCurveId: this.id1,
          trimCurvePoint: new Point( 0.5*this.d,0,0),
        });
        //退出编辑
        this.crownCADPlugin.command.sketch.exitEditSketch({sketchName: 'sketch1'});
        //执行编辑
        this.crownCADPlugin.command.execute().then(() => {
          //获取分度圆和渐开线交点
          this.getPolygonPoint().then(()=> {
            //获取创建内多边形
            this.createPolygon();
          });
        });
      },
      //获取分度圆和渐开线交点
      getPolygonPoint() {
        return new Promise((resolve, reject) => {
          this.crownCADPlugin.query.getFeaturesByNames(['sketch1'])
          // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETFEATURESBYNAMES, {featureNames: ["sketch1"]})
            .then(res => {
              if (res?.data.features) {
                this.crownCADPlugin.query.getElementsByFeatureId(res.data.features[0]?.id)
                // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETElEMENTSBYFEATURE, {featureId: res.data.features[0]?.id})
                  .then(result => {
                    result.data.elements.forEach(element => {
                      //类型为12是曲线（裁剪分度圆得到的曲线），起点为渐开线和圆的交点
                      if (element.type === 8 && element.params.type ===12) {
                        this.q1.x= element.params.startPoint['0']
                        this.q1.y= element.params.startPoint['1']
                      }
                    })
                    resolve();
                  })
              }
            })
        })
      },

      //获取创建内多边形
      createPolygon() {
        this.crownCADPlugin.command.clearCommand();
        //进入编辑操作
        this.crownCADPlugin.command.sketch.editSketch({sketchName: 'sketch1'});
        //创建内多边形
        const q1 = new Point(this.q1.x, this.q1.y);
        this.crownCADPlugin.command.sketch.createRegularPolygon({
          centerPoint: new Point(0,0,0),
          startPoint: q1,
          edgeNum: 2* this.z,
          mode: 0,
        })
        //退出编辑操作
        this.crownCADPlugin.command.sketch.exitEditSketch({sketchName: 'sketch1'});
        this.crownCADPlugin.command.execute().then(() => {
          this.crownCADPlugin.command.clearCommand();
          //创建渐开线镜像，需要先创建镜像线，镜像线为内多边形一边的中点该方法获取中点
          this.getAxisPoint().then(()=> {
            //创建镜像线
            this.createLine()
          })
        })
      },
      //创建渐开线镜像，需要先创建镜像线，镜像线为内多边形一边的中点该方法获取中点
      getAxisPoint() {
        return new Promise((resolve, reject) => {
          this.crownCADPlugin.query.getFeaturesByNames(['sketch1'])
          // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETFEATURESBYNAMES, {featureNames: ["sketch1"]})
            .then(res => {
              if (res?.data.features) {
                this.crownCADPlugin.query.getElementsByFeatureId(res.data.features[0]?.id)
                // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETElEMENTSBYFEATURE, {featureId: res.data.features[0]?.id})
                  .then(result => {
                    result.data.elements.forEach(element => {
                      //内外花键获取点的方法
                      if(element.type===8 && element.params.endPoint !== null) {
                        if(this.q1.x.toFixed(5)=== element.params.endPoint['0'].toFixed(5)) {
                          //mP存储内接多边形一边的中点为镜像线
                          this.mP=  element.params.middlePoint
                          this.q1.x= element.params.endPoint['0']
                          this.q1.y= element.params.endPoint['1']
                        }
                      }
                      //类型10为直线，id2存储所有内接多边形的id，后期会删除这些线
                      if(element.type===8 && element.params.type=== 10 ) {
                        this.id2.push(element.id)
                      }
                    })
                    resolve();
                  })
              }
            })
        })
      },

      //创建镜像线
      createLine() {
        this.crownCADPlugin.command.clearCommand();
        this.crownCADPlugin.command.sketch.editSketch({sketchName: 'sketch1'});
        // 创建镜像线
        this.crownCADPlugin.command.sketch.createSketchLine({
          startPoint: new Point(0,0,0),
          endPoint:new Point( this.mP['0'],this.mP['1'],0)
        })
        this.crownCADPlugin.command.sketch.exitEditSketch({sketchName: 'sketch1'});
        this.crownCADPlugin.command.execute().then(() => {
          this.crownCADPlugin.command.clearCommand();
          //获取镜像线id
          this.getLineId().then(()=>{
            //创建渐开线镜像
            this.createEquationCurveMirror()
          })
        })

      },

      //获取镜像线id
      getLineId() {
        return new Promise((resolve, reject) => {
          this.crownCADPlugin.query.getFeaturesByNames(['sketch1'])
          // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETFEATURESBYNAMES, {featureNames: ["sketch1"]})
            .then(res => {
              if (res?.data.features) {
                this.crownCADPlugin.query.getElementsByFeatureId(res.data.features[0]?.id)
                // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETElEMENTSBYFEATURE, {featureId: res.data.features[0]?.id})
                  .then(result => {
                    result.data.elements.forEach(element => {
                      if (element.type === 8 && element.params.endPoint !== null) {
                        if (element.params.endPoint['0'] === this.mP['0']) {
                          this.axisId = element.id; //镜像线id
                          this.axisLen= element.params.length; //镜像线长度，后面计算圆齿根3点圆中间点使用
                        }
                      }
                    })
                    resolve();
                  })
              }
            })
        })
      },

      //创建渐开线镜像
      createEquationCurveMirror() {
        this.crownCADPlugin.command.clearCommand();
        this.crownCADPlugin.command.sketch.editSketch({sketchName: 'sketch1'});

        // 选中镜像轴和渐开线，使镜像线和x轴对齐
        this.crownCADPlugin.command.sketch.rotateElements({
          elementIds: [this.axisId,this.EquationId],
          centerPoint: new Point(0,0,0),
          angle: Math.atan(Math.abs(this.mP['1']/this.mP['0'])) * 180 / Math.PI,
        })

        //创建渐开线镜像
        this.crownCADPlugin.command.sketch.createMirror({
          elementIds: [this.EquationId],
          axisId: this.axisId,
        })
        this.crownCADPlugin.command.sketch.setReference({curveIds: [this.axisId], reference: 1})
        this.crownCADPlugin.command.sketch.exitEditSketch({sketchName: 'sketch1'});
        this.crownCADPlugin.command.execute().then(() => {
          this.crownCADPlugin.command.clearCommand();
          //获取镜像线信息
          this.getMirroLine().then(()=>{
            //裁剪各种线
            this.cutLine();
          })
        })
      },
      //获取镜像线信息
      getMirroLine() {
        return new Promise((resolve, reject) => {
          this.crownCADPlugin.query.getFeaturesByNames(['sketch1'])
          // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETFEATURESBYNAMES, {featureNames: ["sketch1"]})
            .then(res => {
              if (res?.data.features) {
                this.crownCADPlugin.query.getElementsByFeatureId(res.data.features[0]?.id)
                // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETElEMENTSBYFEATURE, {featureId: res.data.features[0]?.id})
                  .then(result => {
                    result.data.elements.forEach(element => {
                      //类型20为镜像线，循环找到镜像的渐开线的id和起始点
                      if(element.type===8 && element.params.type=== 20) {
                        if(this.EquationId!== element.id) {
                          this.EquationId1= element.id;
                          this.EquationP1= element.params.endPoint;
                          this.EquationS1= element.params.startPoint;
                        } else {
                          this.EquationP= element.params.endPoint;
                          this.EquationS= element.params.startPoint;
                        }
                      }
                    })
                    resolve();
                  })
              }
            })
        })
      },
      //裁剪各种线
      cutLine() {
        this.crownCADPlugin.command.clearCommand();
        this.crownCADPlugin.command.sketch.editSketch({sketchName: 'sketch1'});
        //裁剪已画的圆，裁剪至和渐开线、内多边形的交点
        this.crownCADPlugin.command.sketch.createAutoTrimCurve({
          trimCurveId: this.dieid,
          trimCurvePoint: new Point( -0.5*this.die,0,0),
        })
        this.crownCADPlugin.command.sketch.createAutoTrimCurve({
          trimCurveId: this.deeid[0],
          trimCurvePoint: new Point( 0,0.5* this.dee,0),
        })
        this.crownCADPlugin.command.sketch.createAutoTrimCurve({
          trimCurveId: this.dfid[0],
          trimCurvePoint: new Point( 0,0.5* this.df,0),
        })
        //删除分度圆
        this.crownCADPlugin.command.sketch.deleteElements({
          elementIds: this.id1,
        })
        //删除内接多边形
        this.crownCADPlugin.command.sketch.deleteElements({
          elementIds: this.id2,
        })

        this.crownCADPlugin.command.sketch.exitEditSketch({sketchName: 'sketch1'});
        this.crownCADPlugin.command.execute().then(() => {
          this.crownCADPlugin.command.clearCommand();
          //再次获取镜像线信息，更新镜像线新的起始点，方便裁剪
          this.getMirroLine().then(()=>{
            this.crownCADPlugin.command.sketch.editSketch({sketchName: 'sketch1'});
            //裁剪第一条渐近线到终点
            this.crownCADPlugin.command.sketch.createAutoTrimCurve({
              trimCurveId: this.EquationId,
              trimCurvePoint: new Point( this.EquationP['0'],this.EquationP['1'],0),
            })
            this.crownCADPlugin.command.sketch.createAutoTrimCurve({
              trimCurveId: this.EquationId,
              trimCurvePoint: new Point( this.EquationS['0'],this.EquationS['1'],0),
            })
            //裁剪第二条渐近线到终点
            this.crownCADPlugin.command.sketch.createAutoTrimCurve({
              trimCurveId: this.EquationId1,
              trimCurvePoint: new Point( this.EquationP1['0'],this.EquationP1['1'],0),
            })
            this.crownCADPlugin.command.sketch.createAutoTrimCurve({
              trimCurveId: this.EquationId1,
              trimCurvePoint: new Point(this.EquationS1['0'],this.EquationS1['1'],0),
            })
            this.crownCADPlugin.command.sketch.exitEditSketch({sketchName: 'sketch1'});
            this.crownCADPlugin.command.execute().then(() => {
              //获取剩余线的信息
              this.getLeaveLine().then(()=> {
                this.crownCADPlugin.command.clearCommand();
                this.crownCADPlugin.command.sketch.editSketch({sketchName: 'sketch1'});
                // 镜像线和齿顶圆的交点
                this.p1 = new Point(this.poel[0].params.middlePoint['0'], this.poel[0].params.middlePoint['1']);
                // 删除齿顶圆剩余的那段圆弧
                this.crownCADPlugin.command.sketch.deleteElements({
                  elementIds: [this.poel[0].id],
                })
                this.crownCADPlugin.command.sketch.exitEditSketch({sketchName: 'sketch1'});
                this.crownCADPlugin.command.execute().then(() => {
                  //创建花键轴草图
                  this.createSplineShaft()
                })
              })
            })
          })
        })
      },

      //获取剩余线的信息
      getLeaveLine() {
        return new Promise((resolve, reject) => {
          this.crownCADPlugin.query.getFeaturesByNames(['sketch1'])
          // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETFEATURESBYNAMES, {featureNames: ["sketch1"]})
            .then(res => {

              if (res?.data.features) {
                this.crownCADPlugin.query.getElementsByFeatureId(res.data.features[0]?.id)
                // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETElEMENTSBYFEATURE, {featureId: res.data.features[0]?.id})
                  .then(result => {
                    //找到所有裁剪圆剩余的曲线
                    let poel= []
                    result.data.elements.forEach(element => {
                      if(element.type===8 && element.params.type=== 12 && element.params.middlePoint['0']> this.mP['0']) {
                        poel.push(element);
                      }
                    })

                    if(poel[0].params.middlePoint['0']> poel[1].params.middlePoint) {
                      this.poel.push(poel[1])
                    } else {
                      this.poel.push(poel[0])
                    }

                    resolve();
                  })
              }
            })
        })
      },
      //创建花键轴草图
      createSplineShaft() {
        this.crownCADPlugin.command.clearCommand();
        //上视基准面上创建sketch2草图
        this.crownCADPlugin.command.sketch.createSketch({sketchName: 'sketch2', datumId: 36});
        //上视基准面上绘制直线
        this.crownCADPlugin.command.sketch.createSketchLine({
          startPoint:  new Point(this.poel[0].params.middlePoint['0'], 0, 0),
          endPoint: new Point(this.poel[0].params.middlePoint['0'], -1* this.t, 0)
        })
        //上视基准面上画三点圆
        this.crownCADPlugin.command.sketch.createArcByThreePoint({
          firstPoint: new Point(this.poel[0].params.middlePoint['0'], 0, 0),
          secondPoint: new Point(this.poel[0].params.middlePoint['0'], -1* this.t, 0),
          thirdPoint: new Point(this.poel[0].params.middlePoint['0'] + this.h, -0.5* this.t, 0),
        })

        this.crownCADPlugin.command.sketch.exitSketch();
        this.crownCADPlugin.command.execute().then((res) => {
          this.crownCADPlugin.command.clearCommand();

          // 获取上视基准面上的直线和三点圆弧的id
          this.getLLine().then(()=> {
            this.crownCADPlugin.command.sketch.editSketch({sketchName: 'sketch2'});

            // 上视基准面上绘制旋转凸台封闭图形直线
            this.crownCADPlugin.command.sketch.createSketchLine({
              startPoint:  new Point(0, 0, 0),
              endPoint: new Point(this.poel[0].params.middlePoint['0'], 0, 0)
            })

            this.crownCADPlugin.command.sketch.createSketchLine({
              startPoint:  new Point(0, 0, 0),
              endPoint: new Point(0, -1* this.t, 0)
            })

            this.crownCADPlugin.command.sketch.createSketchLine({
              startPoint:  new Point(this.poel[0].params.middlePoint['0'], -1* this.t, 0),
              endPoint: new Point(0, -1* this.t, 0)
            })

            // 上视基准面上删除该条直线，使上面3条直线和3点圆弧形成封闭图形
            this.crownCADPlugin.command.sketch.deleteElements({
              elementIds: this.id3,
            })

            this.crownCADPlugin.command.sketch.exitEditSketch({sketchName: 'sketch2'});
            this.crownCADPlugin.command.execute().then(() => {

              // 创建旋转凸台
              this.createExtrude().then(()=>{
                // 扫描切除得到齿形
                this.createExtrudeCut().then(()=> {
                  // 圆周阵列齿形
                  this.createCircularPattern().then(()=> {
                    // 布尔交运算
                    this.booleanIntersect().then((res=> {
                      this.loading= false
                    }))
                  })

                })
              })
            })
          })
        })
      },
      // 获取上视基准面上的直线和三点圆弧的id
      getLLine() {
        return new Promise((resolve, reject) => {
          this.crownCADPlugin.query.getFeaturesByNames(['sketch2'])
          // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETFEATURESBYNAMES, {featureNames: ["sketch2"]})
            .then(res => {

              if (res?.data.features) {
                this.crownCADPlugin.query.getElementsByFeatureId(res.data.features[0]?.id)
                // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETElEMENTSBYFEATURE, {featureId: res.data.features[0]?.id})
                  .then(result => {
                    result.data.elements.forEach(element => {
                      // p1点在上视基准面上画的直线
                      if(element.type===8 && element.params.type=== 10) {
                        this.id3.push(element.id)
                      }

                      // 上视基准面上3点圆弧的id
                      if(element.type===8 && element.params.type=== 12) {
                        this.id4.push(element.id)
                      }
                    })
                    resolve();
                  })
              }
            })
        })
      },

      //旋转凸台
      createExtrude() {
        this.crownCADPlugin.command.clearCommand();
        return new Promise((resolve, reject) => {
          this.crownCADPlugin.query.getFeaturesByNames(['sketch2'])
          // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETFEATURESBYNAMES, {featureNames: ["sketch2"]})
            .then(res => {
            if (res?.data.features) {
              this.crownCADPlugin.query.getEntitiesByFeatureIds([res.data.features[0]?.id])
              // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETENTITIESBYFEATUREIDS, {featureIds: [res.data.features[0]?.id]})
                .then(res => {
                if (res?.data.entities) {
                  // 旋转凸台中心轴
                  let basePnt = new Point(0, 0, 0);
                  let endPnt = new Point(0, 0, -10);
                  this.axis = new Axis();
                  this.axis.setPoint(basePnt, endPnt);

                  this.crownCADPlugin.command.solid.revolve({
                    sketch: res.data.entities[0]?.id,
                    axis: this.axis,
                    reverse: 0,
                    revolveType1: 0,
                    angle1: 360,
                    toSurface1: 0, },"旋转凸台")
                  this.crownCADPlugin.command.execute().then(() => {
                    return resolve();
                  }).catch(e=>{
                  });
                } else {
                  return reject();
                }
              });
            } else {
              return reject();
            }
          });
        });
      },

      // 扫描切除得到齿形
      createExtrudeCut() {
        this.crownCADPlugin.command.clearCommand();
        return new Promise((resolve, reject) => {
          //获取草图sketch1中获得的齿形
          this.crownCADPlugin.query.getFeaturesByNames(['sketch1'])
          // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETFEATURESBYNAMES, {featureNames: ["sketch1"]})
            .then(res => {
            if (res?.data.features) {
              this.crownCADPlugin.query.getEntitiesByFeatureIds([res.data.features[0]?.id])
              // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETENTITIESBYFEATUREIDS, {featureIds: [res.data.features[0]?.id]})
                .then(res => {
                if (res?.data.entities) {
                  //获取旋转凸台的特征
                  this.crownCADPlugin.query.getFeaturesByNames(['旋转凸台'])
                  // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETFEATURESBYNAMES, {featureNames: ["旋转凸台"]})
                    .then(extrudeRes => {
                    if (extrudeRes?.data.features) {
                      this.crownCADPlugin.query.getEntitiesByFeatureIds([extrudeRes.data.features[0]?.id])
                      // this.crownCADPlugin.request.execFunction(PluginFunctionType.GETENTITIESBYFEATUREIDS, {featureIds: [extrudeRes.data.features[0]?.id]})
                        .then(ferRes => {
                        if (ferRes?.data.entities) {
                          //扫描切除齿形
                          this.crownCADPlugin.command.solid.sweepCut({
                            sweepType: 0,
                            sweepProfile:res.data.entities[0]?.id,
                            sweepPath:[this.id4[0]],
                            sweepDirection: 0,
                            profileDirection:0,
                            cutSolids: [ferRes.data.entities[0]?.id]
                          }, "扫描切除");
                          this.crownCADPlugin.command.execute().then(() => {
                            return resolve();
                          });
                        } else {
                          return reject();
                        }
                      });
                    } else {
                      return reject();
                    }
                  });
                } else {
                  return reject();
                }
              });
            } else {
              return reject();
            }
          });
        });
      },

      // 圆周阵列齿形
      createCircularPattern() {
        this.crownCADPlugin.command.clearCommand();
        return new Promise((resolve, reject) => {
          this.crownCADPlugin.query.getEntitiesByFeatureNames(['扫描切除'])
          // this.crownCADPlugin.request.execFunction(PluginFunctionType.getEntitiesByFeatureNames, {featureNames: ["扫描切除"]})
            .then(res => {
            if (res?.data.entities) {
              //圆周阵列
               this.crownCADPlugin.command.solid.circularPattern({
                baseType: 0,
                // features: [res.features[0]?.id],
                body:res.data.entities[0]?.id,
                axis:  this.axis,
                angle1: 360,
                instanceNum1: this.z,
                equalSpacing1: 1
              }, "圆周阵列");

              this.crownCADPlugin.command.execute().then(() => {
                return resolve();
              }).catch(e=>{
              });
            } else {
              return reject();
            }
          });
        });
      },
      // 布尔交运算
      booleanIntersect() {
        this.crownCADPlugin.command.clearCommand();
        //创建阵列中心轴
        return new Promise((resolve, reject) => {
          // 获取所以实体信息
          this.crownCADPlugin.query.getEntities().then((res)=> {
            // 留下布尔交的实体
            let entityId= []

            res.data.entities.forEach(entity => {
              if(entity.name && entity.name!=='X轴' && entity.name!=='Y轴'&& entity.name!=='Z轴'&& entity.name!=='sketch1'&& entity.name!=='sketch2')
              entityId.push(entity.id)
            })

            this.crownCADPlugin.command.solid.booleanIntersect({
              entitys: entityId,
              keep: 0, },"布尔交")
            this.crownCADPlugin.command.execute().then(() => {
              return resolve();
            }).catch(e=>{

            });
          })
        })
      }
    }
  }
</script>

<style scoped>
  >>> .el-form-item {
    margin-bottom: 0!important;
  }

  >>> .el-form-item__label {
    margin-bottom: 0!important;
  }
</style>
